-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly Circular Linked List - Merge two unsorted lists into a sorted list.cpp
122 lines (110 loc) · 2.73 KB
/
Doubly Circular Linked List - Merge two unsorted lists into a sorted list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using namespace std;
struct NODE {
int data;
NODE *prev;
NODE *next;
};
class List {
public:
NODE *prevnode = NULL;
NODE *head = NULL;
NODE *tail = NULL;
};
void add(List &obj, int value){
if(obj.head == NULL){
obj.head = obj.tail = new NODE;
obj.head->data = value;
obj.head->prev = obj.head->next = obj.tail;
} else {
obj.prevnode = obj.tail;
obj.tail->next = obj.head->prev = new NODE;
obj.tail = obj.tail->next;
obj.tail->prev = obj.prevnode;
obj.tail->data = value;
obj.tail->next = obj.head;
}
}
void selectionSort_ASC(NODE* head){
NODE *temp;
if(head == NULL){
cout << "Sorting not possible!" << endl;
} else {
for(NODE *x = head; x->next != head; x = x->next){
temp = x;
for(NODE *y = x->next; y != head; y = y->next){
if(y->data < temp->data) temp = y;
}
if(temp != x){
int swap = x->data;
x->data = temp->data;
temp->data = swap;
}
}
}
}
List Sorted;
void mergeList(List &listA, List &listB){
NODE *A = listA.head, *B = listB.head;
if(A || B){
NODE *root = new NODE;
NODE *sort = root;
while(!(A == 0 && B == 0)){
if((A && B && A->data <= B->data) || (A && !B)){
sort->next = A;
A = A->next;
if(A == listA.head) A = 0;
} else if ((A && B && A->data > B->data) || (!A && B)) {
sort->next = B;
B = B->next;
if(B == listB.head) B = 0;
}
Sorted.prevnode = sort;
sort = sort->next;
sort->prev = Sorted.prevnode;
}
Sorted.head = root->next;
Sorted.tail = sort;
Sorted.tail->next = Sorted.head;
Sorted.head->prev = Sorted.tail;
} else {
cout << "Merge sort not possible" << endl;
}
}
int print(List list){
NODE *ref = list.head;
if(ref == 0){
cout << "[EMPTY LIST]" << endl;
return false;
} else {
do {
cout << ref->data;
ref = ref->next;
if(ref != list.head) cout << " => ";
} while(ref != list.head);
cout << endl;
return true;
}
}
int main(){
List A, B;
add(A, 15);
add(A, 22);
add(A, 28);
add(A, 20);
add(A, 48);
add(B, 18);
add(B, 100);
add(B, 85);
add(B, 42);
add(B, 25);
cout << "[A] ";
print(A);
cout << "[B] ";
print(B);
selectionSort_ASC(A.head);
selectionSort_ASC(B.head);
mergeList(A, B);
cout << "[S] ";
print(Sorted);
}